<template>
    <div class="interest-form-container">
        <!-- 页面头部 -->
        <div class="page-header">
            <div class="header-content">
                <div class="header-left">
                    <h2>{{ isEdit ? '编辑兴趣' : '新增兴趣' }}</h2>
                </div>
                <div class="header-right">
                    <el-button @click="handleCancel">
                        <el-icon><ArrowLeft /></el-icon>
                        返回
                    </el-button>
                </div>
            </div>
        </div>

        <!-- 表单内容 -->
        <div class="form-content">
            <el-card shadow="never" class="form-card">
                <el-form
                    ref="interestFormRef"
                    :model="formData"
                    :rules="formRules"
                    label-width="120px"
                    class="interest-form"
                >
                    <!-- 兴趣名称 -->
                    <el-form-item label="兴趣名称:" prop="interestName">
                        <el-input
                            v-model="formData.interestName"
                            placeholder="输入兴趣名称"
                            style="width: 300px"
                        />
                        <div class="form-hint">属性名称,不能为空。中文2-8长度</div>
                    </el-form-item>

                    <!-- 备注 -->
                    <el-form-item label="备注:" prop="remarks">
                        <el-input
                            v-model="formData.remarks"
                            type="textarea"
                            :rows="6"
                            placeholder="请输入备注信息"
                            style="width: 500px"
                        />
                    </el-form-item>

                    <!-- 操作按钮 -->
                    <el-form-item>
                        <div class="form-actions">
                            <el-button @click="handleCancel">取消</el-button>
                            <el-button type="primary" @click="handleSubmit">保存</el-button>
                        </div>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ArrowLeft } from '@element-plus/icons-vue';

const route = useRoute();
const router = useRouter();

const id = route.params.id as string;
const isEdit = computed(() => !!id);

// 表单引用
const interestFormRef = ref();

// 表单数据
const formData = reactive({
    interestName: '',
    remarks: ''
});

// 表单验证规则
const formRules = {
    interestName: [
        { required: true, message: '属性名称不能为空', trigger: 'blur' },
        { min: 2, max: 8, message: '中文长度2-8个字符', trigger: 'blur' }
    ],
    remarks: [
        { required: true, message: '请输入备注', trigger: 'blur' }
    ]
};

// 提交表单
const handleSubmit = async () => {
    if (!interestFormRef.value) return;
    
    try {
        await interestFormRef.value.validate();
        
        ElMessageBox.confirm(
            `确定要${isEdit.value ? '更新' : '保存'}此兴趣吗？`,
            '确认操作',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'success',
            }
        ).then(() => {
            ElMessage.success(`${isEdit.value ? '更新' : '保存'}成功`);
            router.push('/interest-list');
        }).catch(() => {
            ElMessage.info('已取消操作');
        });
    } catch (error) {
        ElMessage.error('请检查表单信息');
    }
};

// 取消操作
const handleCancel = () => {
    router.push('/interest-list');
};

// 页面初始化
onMounted(() => {
    if (isEdit.value) {
        // 编辑模式：加载现有数据
        loadInterestData(id);
    }
});

// 加载兴趣数据
const loadInterestData = (interestId: string) => {
    // 模拟加载数据
    console.log('加载兴趣数据，ID:', interestId);
    // 这里可以调用API加载数据
    // 示例数据
    Object.assign(formData, {
        interestName: '兴趣名称1',
        remarks: '这是一个示例兴趣的备注信息'
    });
};
</script>

<style scoped>
.interest-form-container {
    padding: 20px;
    background-color: #f5f5f5;
    min-height: 100vh;
}

.page-header {
    background: white;
    padding: 20px;
    border-radius: 8px;
    margin-bottom: 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header-left h2 {
    margin: 0;
    color: #333;
    font-size: 24px;
}

.form-content {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.form-card {
    border-radius: 8px;
}

.interest-form {
    padding: 30px;
}

.form-hint {
    color: #f56c6c;
    font-size: 12px;
    margin-top: 4px;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .interest-form-container {
        padding: 10px;
    }
    
    .header-content {
        flex-direction: column;
        gap: 16px;
        align-items: flex-start;
    }
    
    .interest-form {
        padding: 20px;
    }
    
    .form-actions {
        justify-content: center;
    }
}
</style>
